package order.dao.impl;

import order.dao.BaseDao;
import order.dao.OrderbiaoDao;
import order.entity.Orderbiao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderbiaoDaoImpl extends BaseDao implements OrderbiaoDao {
    @Override
    public List<Orderbiao> getList(int page, int pageSize) {
        String sql="select * from Orderbiao ORDER BY Pickup=0 DESC limit ?,? ";
        ResultSet rs=this.executeQuery(sql,(page-1)*pageSize,pageSize);
        List<Orderbiao> list=new ArrayList<>();

        try {
            while (rs.next()){
                list.add(new Orderbiao(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getString(5),rs.getInt(6),rs.getInt(7),rs.getInt(8),rs.getString(9)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            this.closeConnection(null,null,rs);
        }
        return list;
    }

    @Override
    public int UpOrder(Orderbiao o) {
        String sql="UPDATE `BurgerKingDB`.`Orderbiao` SET `Pickup` = 1,Pickuptime=now() WHERE `id` =? and `Pickup` = 0";
        return this.executeUpdate(sql,o.getId());
    }

    @Override
    public int getCount() {
        String sql="select count(*) from Orderbiao";
        ResultSet rs=this.executeQuery(sql);
        try {
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally {
            closeConnection(null,null,rs);
        }
        return 0;
    }

    @Override
    public List<Orderbiao> getLike(int index) {
        String sql="select * from Orderbiao where id=?";
        ResultSet rs=this.executeQuery(sql,index);
        List<Orderbiao> list=new ArrayList<>();
        try {
            while (rs.next()){
                list.add(new Orderbiao(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getString(5),rs.getInt(6),rs.getInt(7),rs.getInt(8),rs.getString(9)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            this.closeConnection(null,null,rs);
        }
        return list;
    }
}
